﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace SpaceEngine.Assets
{
    public class Line : DrawableGameComponent
    {

        Game1 game;
        short[] lineIndices;
        VertexPositionColor[] points;
        BasicEffect effect;
        VertexDeclaration vertexDecl;

        public Line(Game1 game, VertexPositionColor[] points)
            : base(game)
        {
            this.game = game;
            this.points = points;

            // indices reference vertex buffer
            lineIndices = new short[points.Length];
            for (int i = 0; i < points.Length; i++)
            {
                lineIndices[i] = (short)(i);
            }
        }

        private void InitializeEffect()
        {

            vertexDecl = new VertexDeclaration(
                game.graphics.GraphicsDevice,
                VertexPositionColor.VertexElements
                );

            effect = new BasicEffect(game.graphics.GraphicsDevice, null);
        }

        public override void Initialize()
        {
            InitializeEffect();
            base.Initialize();
        }

        public override void Draw(GameTime gameTime)
        {
            game.graphics.GraphicsDevice.VertexDeclaration = vertexDecl;
            game.graphics.GraphicsDevice.RenderState.PointSize = 10;
            effect.Begin();

            effect.VertexColorEnabled = true;
            effect.World = game.camera.World;
            effect.View = game.camera.View;
            effect.Projection = game.camera.Projection;

            // turn on alpha blending
            game.graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
            game.graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.SourceAlpha;
            game.graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;

            Console.WriteLine(lineIndices.Length);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                game.graphics.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
                    PrimitiveType.LineStrip,
                    points, // the vertex buffer
                    0, // buffer offset
                    points.Length, // number of vertices in list
                    lineIndices, // the index buffer
                    0, // starting index
                    lineIndices.Length - 1 // the number of primatives to draw
                    );
                /*game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                    PrimitiveType.PointList,
                    points,
                    0,
                    1
                    );*/
                game.GraphicsDevice.RenderState.FillMode = FillMode.Solid;
                pass.End();
            }
            effect.End();

            game.graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;

            base.Draw(gameTime);
        }
    }
}
